﻿Imports System.Text.RegularExpressions
Imports System.Net
Imports System.Collections.Specialized

Public Class UpdateChecker
	Public Event UpdateStatus(NewVersion As String)

	Public Sub CheckVersion()
		Try
			Using client As New System.Net.WebClient
				Dim Page As String = client.DownloadString("http://secretarytool.codeplex.com/")

				Dim regexVersionMatch As New Regex("(?<=right_sidebar.*)[0-9]\.[0-9]\.[0-9]", RegexOptions.Singleline)
				Dim regexMatchResult As Match = regexVersionMatch.Match(Page)

				If regexMatchResult.Success Then
					IsNewerVersion(New Version(regexMatchResult.Value))
				End If

			End Using

		Catch ex As Exception
			'there is something wrong with the version checking; ignore it
		End Try

	End Sub

	Private Sub IsNewerVersion(WebVersion As Version)

		Select Case WebVersion.CompareTo(New Version(System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly.Location).FileVersion.ToString))
			Case 0
				'the same
			Case 1
				'WebVersion is newer than the current version
				RaiseEvent UpdateStatus(WebVersion.ToString)
			Case -1
				'WebVersion is older than the current version
		End Select

	End Sub
End Class
